<?php

declare(strict_types=1);

namespace App\JsonRpc\Service;

use App\Constants\CommonServerConstant;
use App\JsonRpc\RpcOrderServiceInterface;
use App\Services\TestService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\RpcServer\Annotation\RpcService;

#[RpcService(name: 'RpcOrderService', protocol: 'jsonrpc-http', server: CommonServerConstant::SERVER_JSONRPC_HTTP)]
class RpcOrderService implements RpcOrderServiceInterface
{
    #[Inject]
    protected TestService $testService;

    public function router(string $class, string $method, mixed $params = null): mixed
    {
        $result = null;
        try {
            $result = match ($class) {
                'TestService' => $this->testService->router($method, $params),
                default => throw new \Exception('当前类不存在'),
            };
        } catch (\Throwable $th) {
            // 异常不能吞掉
            throw $th;
        }

        return $result;
    }

    public function health($string): string
    {
        return 'order_service_ok_'.$string;
    }
}
